<?php

namespace app\common\model\product\order;

use think\Model;

class Product extends Model
{
    // 数据表
    protected $name = 'order_product';

    protected $append = [
        'comtime_text',
        'thumbs_text',
        'query_time_text',
        'query_qrcode_cdn'
    ];

    public function getThumbsTextAttr($value, $data)
    {
        //获取域名部分
        $domain = request()->domain();
        $domain = trim($domain, '/');

        $thumbs = isset($data['thumbs']) ? $data['thumbs'] : '';

        //如果为空就给一个默认图片地址
        if (empty($thumbs) || !is_file("." . $thumbs)) {
            $thumbs = "/assets/home/images/video.jpg";
        }

        return $domain . $thumbs;
    }

    public function getComtimeTextAttr($value, $data)
    {
        $comtime = isset($data['comtime']) ? trim($data['comtime']) : "";

        if (empty($comtime)) {
            return '暂无评论时间';
        }

        return date("Y-m-d", $comtime);
    }

    // 关联用户查询
    public function products()
    {
        return $this->belongsTo('app\common\model\Product\Product', 'proid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function business()
    {
        return $this->belongsTo('app\common\model\Business\Business', 'busid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getQueryTimeTextAttr($value, $data)
    {
        $query_time = $data['querytime'] ?? '';

        if (empty($query_time)) {
            return '未查询';
        }

        return date('Y-m-d', $query_time);
    }

    public function getQUeryQrcodeCdnAttr($value, $data)
    {
        $qrcode = $data['query_qrcode'] ?? '';

        if (!$qrcode || !is_file('.' . $qrcode)) {
            return null;
        }

        $domain = request()->domain();

        return $domain . $qrcode;
    }
}
